package com.rxh.controller;

import com.rxh.pojo.base.SearchInfo;
import com.rxh.pojo.base.Page;
import com.rxh.pojo.base.PageResult;
import com.rxh.pojo.core.CoreCargoTrack;
import com.rxh.pojo.core.CoreMerchantOrder;
import com.rxh.pojo.core.CoreOrder;
import com.rxh.pojo.core.CoreOrderChange;
import com.rxh.pojo.merchant.MerchantInfo;
import com.rxh.pojo.sys.SysConstant;
import com.rxh.service.ConstantService;
import com.rxh.service.MerchantService;
import com.rxh.service.TransactionService;
import com.rxh.spring.annotation.SystemLogInfo;
import com.rxh.util.ExcelUtils;
import com.rxh.util.UserInfoUtils;
import com.rxh.utils.SystemConstant;
import com.rxh.vo.VoCoreOrder;
import com.rxh.vo.VoCoreOrderChange;
import com.rxh.vo.VoCoreOrderExport;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 陈俊雄
 * Date: 2018/3/1
 * Time: 16:50
 * Project: Management
 * Package: com.rxh.controller
 */
@Controller
@RequestMapping(value = "/trans")
public class TransactionController {

    @Resource
    private TransactionService transactionService;
    @Resource
    private ConstantService constantService;
    @Resource
    private MerchantService merchantService;

    /**
     * 获取交易信息
     *
     * @param page 分页对象
     * @return 查询结果
     */
    @SystemLogInfo(description = "订单查询")
    @RequestMapping(value = "/getTransInfo")
    @ResponseBody
    public PageResult getItemList(@RequestBody Page page) {
        page.getSearchInfo().setMerId(UserInfoUtils.getMerchantId());
        return transactionService.getMerchantOrderList(page);
    }

    /**
     * 获取订单统计信息
     *
     * @param searchInfo 查询信息
     * @return 统计结果
     */
    @RequestMapping(value = "/getStatistics")
    @ResponseBody
    public Map<String, Object> getMerchantTransStatistics(@RequestBody SearchInfo searchInfo) {
        searchInfo.setMerId(UserInfoUtils.getMerchantId());
        return transactionService.getMerchantTransStatistics(searchInfo);
    }

    /**
     * 获取初始化信息
     *
     * @return 初始化信息
     */
    @RequestMapping(value = "/getInquireInit")
    @ResponseBody
    public Map<String, Object> getInquireInit() {
        return transactionService.getMerchantInquirerInit(UserInfoUtils.getMerchantId());
    }

    @RequestMapping(value = "/getOrderCargoTrack")
    @ResponseBody
    public CoreCargoTrack getOrderCargoTrack(@RequestBody Long orderId) {
        return transactionService.getOrderCargoTrack(orderId);
    }

    /**
     * 获取订单详情
     *
     * @param order 订单对象
     * @return 订单详情
     */
    @SystemLogInfo(description = "获取订单详情")
    @RequestMapping(value = "/getDetail")
    @ResponseBody
    public Map<String, Object> getDetail(@RequestBody CoreOrder order) {
        return transactionService.getMerchantOrderDetail(order);
    }

    /**
     * 导出excel信息
     *
     * @param searchInfo 查询信息
     */
    @SystemLogInfo(description = "导出订单信息")
    @RequestMapping(value = "/downloadOrderInfo", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadOrderInfo(HttpServletResponse response, @RequestBody(required = false) SearchInfo searchInfo) throws IOException {
        searchInfo.setMerId(UserInfoUtils.getMerchantId());
        List<VoCoreOrderExport> orderExports = transactionService.getExportOrderInfo(searchInfo);
        String[] groupNames = {SystemConstant.BANK_STATUS, SystemConstant.EXCEPTION_STATUS};
        MerchantInfo merchantInfo = merchantService.selectMerchantInfoByMerchantId(UserInfoUtils.getMerchantId());
        Map<String, Map<String, SysConstant>> constants = constantService.getConstantsMapByGroupNames(groupNames);
        ExcelUtils.exportOrderInfoToExcel(response, orderExports, constants, merchantInfo);
    }

    @SystemLogInfo(description = "导出退款信息")
    @RequestMapping(value = "/downloadRefundInfo", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadRefundInfo(HttpServletResponse response, @RequestBody(required = false) SearchInfo searchInfo) throws IOException {
        searchInfo.setMerId(UserInfoUtils.getMerchantId());
        if (searchInfo.getChangeType() == null) {
            searchInfo.setChangeTypeList(Arrays.asList(SystemConstant.FULL_REFUND, SystemConstant.PARTIAL_REFUND));
        }
        List<VoCoreOrderChange> orderChangeList = transactionService.getExportOrderChangeInfo(searchInfo);
        String[] groupNames = {SystemConstant.EXCEPTION_STATUS, SystemConstant.AUDITED_STATUS};
        MerchantInfo merchantInfo = merchantService.selectMerchantInfoByMerchantId(UserInfoUtils.getMerchantId());
        Map<String, Map<String, SysConstant>> constants = constantService.getConstantsMapByGroupNames(groupNames);
        ExcelUtils.exportRefundInfoToExcel(response, orderChangeList, constants, merchantInfo);
    }

    @SystemLogInfo(description = "导出拒付信息")
    @RequestMapping(value = "/downloadRefusedInfo", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadRefusedInfo(HttpServletResponse response, @RequestBody(required = false) SearchInfo searchInfo) throws IOException {
        searchInfo.setMerId(UserInfoUtils.getMerchantId());
        searchInfo.setChangeTypeList(Arrays.asList(SystemConstant.FULL_REFUSED, SystemConstant.PARTIAL_REFUSED, SystemConstant.PROXY_REFUSED));
        List<VoCoreOrderChange> orderChangeList = transactionService.getExportOrderChangeInfo(searchInfo);
        String[] groupNames = {SystemConstant.EXCEPTION_STATUS, SystemConstant.AUDITED_STATUS};
        MerchantInfo merchantInfo = merchantService.selectMerchantInfoByMerchantId(UserInfoUtils.getMerchantId());
        Map<String, Map<String, SysConstant>> constants = constantService.getConstantsMapByGroupNames(groupNames);
        ExcelUtils.exportRefusedInfoToExcel(response, orderChangeList, constants, merchantInfo);
    }

    @SystemLogInfo(description = "导出保证金信息")
    @RequestMapping(value = "/downloadBondInfo")
    public void downloadBondInfo(HttpServletResponse response, @RequestBody(required = false) SearchInfo searchInfo) throws IOException {
        searchInfo.setMerId(UserInfoUtils.getMerchantId());
        List<VoCoreOrder> orders = transactionService.getExportBondInfo(searchInfo);
        MerchantInfo merchantInfo = merchantService.selectMerchantInfoByMerchantId(UserInfoUtils.getMerchantId());
        Map<String, SysConstant> bondStatus = constantService.getConstantsMapByGroupName(SystemConstant.BOND_STATUS);
        ExcelUtils.exportBondInfoToExcel(response, orders, bondStatus, merchantInfo);
    }

    /**
     * 获取商户订单信息
     *
     * @param orderId 订单号
     * @return 商户订单信息
     */
    @SystemLogInfo(description = "获取商户订单信息")
    @RequestMapping(value = "/getMerchantOrder")
    @ResponseBody
    public CoreMerchantOrder orderChange(@RequestBody Long orderId) {
        return transactionService.getMerchantOrderByOrderId(orderId);
    }

    @SystemLogInfo(description = "保证金查询")
    @RequestMapping(value = "/getBondInfo")
    @ResponseBody
    public PageResult getBondInfo(@RequestBody Page page) {
        page.getSearchInfo().setMerId(UserInfoUtils.getMerchantId());
        return transactionService.getBondInfo(page);
    }

    @RequestMapping(value = "/getBondStatistics")
    @ResponseBody
    public Map<String, Object> getBondStatistics(@RequestBody SearchInfo searchInfo) {
        searchInfo.setMerId(UserInfoUtils.getMerchantId());
        searchInfo.setBankStatus(SystemConstant.BANK_STATUS_SUCCESS);
        return transactionService.getBondStatistics(searchInfo);
    }

    @RequestMapping(value = "/getBondInit")
    @ResponseBody
    public Map<String, Object> getBondInit() {
        return transactionService.getMerchantBondInit(UserInfoUtils.getMerchantId());
    }

    @SystemLogInfo(description = "订单变更")
    @RequestMapping(value = "/orderChange")
    @ResponseBody
    public boolean orderChange(@RequestBody CoreOrderChange orderChange) {
        orderChange.setApplicant(UserInfoUtils.getUserName());
        return transactionService.orderChange(orderChange);
    }

    @RequestMapping(value = "/getRefundInfo")
    @ResponseBody
    public PageResult getRefundInfo(@RequestBody Page page) {
        page.getSearchInfo().setMerId(UserInfoUtils.getMerchantId());
        if (page.getSearchInfo().getChangeType() == null) {
            page.getSearchInfo().setChangeTypeList(Arrays.asList(SystemConstant.FULL_REFUND, SystemConstant.PARTIAL_REFUND));
        }
        return transactionService.getMerchantOrderChangeList(page);
    }

    @RequestMapping(value = "/getRefusedInfo")
    @ResponseBody
    public PageResult getRefusedInfo(@RequestBody Page page) {
        page.getSearchInfo().setMerId(UserInfoUtils.getMerchantId());
        page.getSearchInfo().setChangeTypeList(Arrays.asList(SystemConstant.FULL_REFUSED, SystemConstant.PARTIAL_REFUSED, SystemConstant.PROXY_REFUSED));
        return transactionService.getMerchantOrderChangeList(page);
    }

    @RequestMapping(value = "/getRefusedStatistics")
    @ResponseBody
    public List<Map<String, Object>> getRefusedStatistics() {
        SearchInfo searchInfo = new SearchInfo();
        searchInfo.setMerId(UserInfoUtils.getMerchantId());
        return transactionService.getRefusedStatistics(searchInfo);
    }
}